package it.amicosmanettone.whatsimage;

import it.amicosmanettone.whatsimage.client.ApiClient;
import it.amicosmanettone.whatsimage.jsonbean.CheckFirstMessage;
import it.amicosmanettone.whatsimage.jsonbean.CheckMessage;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;

public class SplashActivity extends Activity {
	
	private static int SPLASH_TIME_OUT = 2500;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_splash);
		
		String currentVersion = Configuration.APP_VERSION;
		
		String UPGRADE = ApiClient.API_CheckAppVersion(currentVersion);
		
		if(UPGRADE.equals("UPGRADE")){
			
			String textUpgrade = getText(R.string.warning_upgrade).toString();
			
			Intent warning_message = new Intent(getApplicationContext(),
					WarningActivity.class);

			warning_message.putExtra("warningText", textUpgrade);
			warning_message.putExtra("UPGRADE", 1);
			
			warning_message.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
			startActivity(warning_message);
			overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
			finish();
			
		}//End UPGRADE
		else if(UPGRADE.equals("NO_UPGRADE")){
			
			final CheckFirstMessage checkmessagefirst = ApiClient.API_CheckFirstMessage();
			final CheckMessage checkmessageuser = ApiClient.API_CheckMessageForUser();
			
			final TextView textLoad = (TextView)findViewById(R.id.splash_text_load);
			String loadFirst = getText(R.string.splash_load_message).toString();
			textLoad.setText(loadFirst);
			
			final SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
			
			final Intent main = new Intent(this, MainActivity.class);
			
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					
					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							
							String loadMessage = getText(R.string.splash_load_message).toString();

							textLoad.setText(loadMessage);

						}
					});
					
					Boolean SHOW_WELCOME_MESSAGE = sharedPref.getBoolean("show_welcome_message", true);
					final String messageFirst = checkmessagefirst.getMessage();
					final String messageUser = checkmessageuser.getMessage();

					runOnUiThread(new Runnable() {
						@Override
						public void run() {
							
							String loadMessage = getText(R.string.splash_load_images).toString();

							textLoad.setText(loadMessage);

						}
					});
					
					if(SHOW_WELCOME_MESSAGE){
						
						if(messageUser.equals("")){
						
							Intent welcome_message = new Intent(getApplicationContext(),
									WelcomeActivity.class);

							welcome_message.putExtra("messageBody", messageFirst);
							welcome_message.putExtra("warning", false);
						
							welcome_message.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
							startActivity(welcome_message);
							overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
							finish();
							
						}else{
							
							Intent welcome_message = new Intent(getApplicationContext(),
									WelcomeActivity.class);

							welcome_message.putExtra("messageBody", messageFirst);
							welcome_message.putExtra("warning", true);
							welcome_message.putExtra("warningText", messageUser);
						
							welcome_message.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
							startActivity(welcome_message);
							overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
							finish();
							
						}
						
					}else{
						
						if(messageUser.equals("")){
						
							main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
							startActivity(main);
							overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
							finish();
							
						}else{
							
							Intent warning_message = new Intent(getApplicationContext(),
									WarningActivity.class);

							warning_message.putExtra("warningText", messageUser);
							
							warning_message.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
							startActivity(warning_message);
							overridePendingTransition(R.xml.translate_in, R.xml.translate_out);
							finish();
							
						}
						
					}//End show_warning_message
					
				}
			}, SPLASH_TIME_OUT);
			
		}//End else if NO_UPGRADE
		
	}//End onCreate

}//End class
